package com.seal.commons.generator.db;

import com.seal.commons.generator.config.DatabaseConfig;
import com.seal.commons.generator.vo.TableInfo;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * 功能描述: <br/>
 *
 * @author yong.huang
 * @date: 2020-08-10 20:29<br/>
 * @since JDK 1.8
 */
public abstract class AbstractTemplateSql implements TemplateSql{

    protected Connection getConnect(DatabaseConfig databaseConfig){
        Connection conn = null;
        try {
            Class.forName(databaseConfig.getDriverName()).newInstance();
            conn = DriverManager.getConnection(databaseConfig.getUrl(), databaseConfig.getUsername(), databaseConfig.getPassword());
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return conn;
    }



    protected void close(Connection conn, PreparedStatement pstmt, ResultSet rs) throws SQLException {
        if (rs != null)
            rs.close();
        if (pstmt != null)
            pstmt.close();
        if (conn != null)
            conn.close();
    }
}